﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GMS.Dinasty.BO;

namespace GMS.BasicControls
{
    public partial class MyTreeview : TreeView
    {
        public MyTreeview()
        {
            InitializeComponent();
        }
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        private List<Person> _listDataSource;
        public List<Person> ListDatasourse
        {
            get 
            {
               /* foreach (TreeNode TN in this.Nodes)
                { 
                    
                }*/
                return _listDataSource; 
            }
            set 
            {
                _listDataSource = value;
                CreateNodes(_listDataSource);
            }
        }
        private void CreateNodes(List<Person> Persons)
        {
            if (Persons != null)
            {
                foreach (Person P in Persons)
                {
                    TreeNode TN = CreateNode(P);
                    if (P.PersonArr.Count != 0)
                    {
                        CreateChildNodes(P.PersonArr, TN);
                    }
                    this.Nodes.Add(TN);
                }
            }
        }
        private void CreateChildNodes(List<Person> Persons, TreeNode ParentNode)
        {
            foreach (Person P in Persons)
            {
                TreeNode Tn = CreateNode(P);
                CreateChildNodes(P.PersonArr, Tn);
                ParentNode.Nodes.Add(Tn);
            }
        }
        private TreeNode CreateNode(Person person)
        {
            TreeNode TN = new TreeNode();
            TN.Tag = person.IdContact;
            TN.Text = person.Name;
            TN.ToolTipText = person.Note;
            if (person.ParentId !=0 && TN.Parent != null)
            {
                TN.Parent.Tag = person.ParentId;
            }
            return TN;
        }
    }
}
